package org.dromara.answer.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.answer.domain.vo.AiQuestionsVo;
import org.dromara.answer.domain.bo.AiQuestionsBo;
import org.dromara.answer.service.IAiQuestionsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 题目
 *
 * @author Lion Li
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/questions")
public class AiQuestionsController extends BaseController {

    private final IAiQuestionsService aiQuestionsService;

    /**
     * 查询题目列表
     */
    @SaCheckPermission("answer:questions:list")
    @GetMapping("/list")
    public TableDataInfo<AiQuestionsVo> list(AiQuestionsBo bo, PageQuery pageQuery) {
        return aiQuestionsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出题目列表
     */
    @SaCheckPermission("answer:questions:export")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiQuestionsBo bo, HttpServletResponse response) {
        List<AiQuestionsVo> list = aiQuestionsService.queryList(bo);
        ExcelUtil.exportExcel(list, "题目", AiQuestionsVo.class, response);
    }

    /**
     * 获取题目详细信息
     *
     * @param questionId 主键
     */
    @SaCheckPermission("answer:questions:query")
    @GetMapping("/{questionId}")
    public R<AiQuestionsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long questionId) {
        return R.ok(aiQuestionsService.queryById(questionId));
    }

    /**
     * 新增题目
     */
    @SaCheckPermission("answer:questions:add")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiQuestionsBo bo) {
        return toAjax(aiQuestionsService.insertByBo(bo));
    }

    /**
     * 修改题目
     */
    @SaCheckPermission("answer:questions:edit")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiQuestionsBo bo) {
        return toAjax(aiQuestionsService.updateByBo(bo));
    }

    /**
     * 删除题目
     *
     * @param questionIds 主键串
     */
    @SaCheckPermission("answer:questions:remove")
    @Log(title = "题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] questionIds) {
        return toAjax(aiQuestionsService.deleteWithValidByIds(List.of(questionIds), true));
    }
}
